package cn.lger.util;

import org.springframework.web.multipart.MultipartFile;

import java.io.File;
import java.io.IOException;
import java.util.UUID;

/**
 * Code that Changed the World
 * Pro said
 *
 * @author Pro
 * @date 2017-12-15
 */
public class FileUploadUtil {

    private FileUploadUtil(){}

    /**
     * 上传文件
     * @param multipartFile multipartFile
     * @param prefixPath 前缀路径，相对于整个项目中的路径，路径最前面不用加入“/”
     * @param fileName 上传后的文件名
     * @return 上传后最终的相对路径+文件名
     * @throws IOException 有可能空指针异常和IO异常
     */
    public static String upload(MultipartFile multipartFile, String prefixPath, String folder, String fileName) throws IOException {
        if (multipartFile.isEmpty()){
            throw new RuntimeException("文件不能为空");
        }
        folder+="/";
        prefixPath = prefixPath + "/" + folder;
        //得出上传的绝对路径
        File file = new File(prefixPath);
        if (!file.exists()){
            if (file.mkdirs()){
                System.out.println("成功创建目录");
            }
        }
        //获取上传的后缀名
        String suffixName = multipartFile.getOriginalFilename().substring(multipartFile.getOriginalFilename().lastIndexOf("."));
        //新建最终确定的文件
        file = new File(prefixPath+fileName+suffixName);
        multipartFile.transferTo(file);
        return folder+fileName+suffixName;
    }

    /**
     * 随机生成UUID
     * @return UUID
     */
    public static String randomUUID(){
        return UUID.randomUUID().toString().replaceAll("-", "");
    }

    /**
     * 装换 MultipartFile为File到/static/temp文件夹下
     * @param multipartFile multipartFile
     * @param fileName 储存文件名
     * @return File
     * @throws IOException IOException
     */
    public static File uploadToTemp(MultipartFile multipartFile, String prefixPath, String fileName) throws IOException {
        if (multipartFile.isEmpty()){
            throw new RuntimeException("文件不能为空");
        }
        //得出上传的绝对路径
        String uploadPath = prefixPath+"/temp/";
        File file = new File(uploadPath);
        if (!file.exists()){
            if (file.mkdirs()){
                System.out.println("成功创建目录");
            }
        }
        //获取上传的后缀名
        String suffixName = multipartFile.getOriginalFilename().substring(multipartFile.getOriginalFilename().lastIndexOf("."));
        //新建最终确定的文件
        file = new File(uploadPath+fileName+suffixName);
        multipartFile.transferTo(file);
        return file;
    }

    /**
     * 上传file到服务器指定的/static/folder文件夹，并且返回文件路径
     * @param file MultipartFile
     * @param folder String
     * @return path
     */
    public static String uploadFileToFolder(MultipartFile file, String prefixPath, String folder){
        try {
            if (file != null && !file.isEmpty()){
                return upload(file,prefixPath, folder, FileUploadUtil.randomUUID());
            }
            return null;
        } catch (Exception e) {
            e.printStackTrace();
            throw new RuntimeException("文件上传失败");
        }
    }

}
